package br.com.bancodecurriculos.controleacesso.gui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import br.com.bancodecurriculos.controleacesso.dominio.Perfil;
import br.com.bancodecurriculos.controleacesso.dominio.Usuario;
import br.com.bancodecurriculos.controleacesso.negocio.UsuarioServices;
import br.com.bancodecurriculos.pessoa.fisica.gui.PessoaFisicaForm;
import br.com.bancodecurriculos.util.gui.Form;

public class LoginForm extends Form {

	private static final long serialVersionUID = 1L;

	/*
	 * private static final String LOGIN = "login.login"; private static final
	 * String SENHA = "login.senha"; private static final String PERFIL =
	 * "login.perfil"; private static final String ENTRAR = "login.entrar";
	 * private static final String NOVO_USUARIO = "login.novo_usuario"; private
	 * static final String USUARIO_INVALIDO = "login.msg.usuario_invalido";
	 */

	private static final String LOGIN = "Login";
	private static final String SENHA = "Senha";
	private static final String PERFIL = "Perfil";
	private static final String ENTRAR = "Entrar";
	private static final String NOVO_USUARIO = "Novo Usu�rio";
	private static final String USUARIO_INVALIDO = "Usu�rio inv�lido!";

	private JLabel lblLogin;
	private JLabel lblSenha;
	private JLabel lblPerfil;
	private JTextField txtFLogin;
	private JPasswordField pwdSenha;
	private JComboBox<String> cmbbxPerfil;
	private JButton btnEntrar;
	private JButton btnCadastrar;

	public LoginForm() {
		super();
		this.init();
	}

	private void init() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(100, 100, 204, 200);
		this.setLocationRelativeTo(null);
		try {
			this.loadComponents();
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.acoes();
		try {
			this.carregarComboPerfil();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.setVisible(true);
	}

	private void loadComponents() throws IOException {
		this.lblLogin = new JLabel(LOGIN);
		this.lblLogin.setFont(super.getFont());
		this.lblLogin.setBounds(10, 11, 46, 19);
		this.getContentPane().add(this.lblLogin);

		this.txtFLogin = new JTextField();
		lblLogin.setLabelFor(this.txtFLogin);
		this.txtFLogin.setFont(super.getFont());
		this.txtFLogin.setBounds(66, 10, 124, 20);
		this.txtFLogin.setFocusable(true);
		this.getContentPane().add(this.txtFLogin);
		this.txtFLogin.setColumns(10);

		this.lblSenha = new JLabel(SENHA);
		this.lblSenha.setFont(super.getFont());
		this.lblSenha.setBounds(10, 41, 58, 20);
		this.getContentPane().add(this.lblSenha);

		this.pwdSenha = new JPasswordField();
		this.lblSenha.setLabelFor(this.pwdSenha);
		this.pwdSenha.setFont(super.getFont());
		this.pwdSenha.setBounds(66, 41, 124, 20);
		this.getContentPane().add(this.pwdSenha);

		this.lblPerfil = new JLabel(PERFIL);
		this.lblPerfil.setFont(super.getFont());
		this.lblPerfil.setBounds(10, 72, 70, 20);
		this.getContentPane().add(this.lblPerfil);

		this.cmbbxPerfil = new JComboBox<String>();
		this.lblPerfil.setLabelFor(this.cmbbxPerfil);
		this.cmbbxPerfil.setFont(super.getFont());
		this.cmbbxPerfil.setBounds(66, 72, 124, 20);
		this.getContentPane().add(this.cmbbxPerfil);

		this.btnEntrar = new JButton(ENTRAR);
		this.btnEntrar.setFont(super.getFont());
		this.btnEntrar.setBounds(38, 104, 125, 23);
		this.getContentPane().add(this.btnEntrar);

		this.btnCadastrar = new JButton(NOVO_USUARIO);
		this.btnCadastrar.setFont(super.getFont());
		this.btnCadastrar.setBounds(38, 139, 125, 23);
		this.getContentPane().add(this.btnCadastrar);
	}

	private void acoes() {
		this.btnEntrar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String login = txtFLogin.getText();
				String senha = String.valueOf(pwdSenha.getPassword());
				String perfilSelecionado = cmbbxPerfil.getSelectedItem()
						.toString();
				try {
					Perfil perfil = new Perfil(perfilSelecionado);
					Usuario usuario = new Usuario(login, senha, perfil);
					if (UsuarioServices.getInstance().validarUsuario(usuario)) {
						new PessoaFisicaForm();
						dispose();
					} else {
						JOptionPane.showMessageDialog(LoginForm.this,
								USUARIO_INVALIDO, ENTRAR,
								JOptionPane.ERROR_MESSAGE);
					}
				} catch (ClassNotFoundException e1) {
					e1.printStackTrace();
				} catch (NoSuchAlgorithmException e1) {
					e1.printStackTrace();
				} catch (SQLException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});

		this.btnCadastrar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				new CadastrarUsuarioForm();
			}
		});
	}

	private void carregarComboPerfil() throws ClassNotFoundException,
			SQLException, IOException {
		List<Perfil> perfils = UsuarioServices.getInstance().getPerfis();
		for (Perfil perfil : perfils) {
			cmbbxPerfil.addItem(perfil.getNome());
		}
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					new LoginForm();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
